package com.nativetalk.controller.app.v4_3_2;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.common.util.TypeTrasUtil;
import com.nativetalk.dto.ExperienceCourseDto;
import com.nativetalk.service.course.search.CourseSearchService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Peter on 2017/7/7.
 */
@Controller
public class IndexController_V4_3_2 {

    private static Logger logger = Logger.getLogger(IndexController_V4_3_2.class);

    @Autowired
    private CourseSearchService courseSearchService;

    /**
     * @return RetInfo
     * @Purpose 体验课程列表
     * @version 4.3.2
     * @time 2017-08-24
     * @author Peter
     */
    @RequestMapping(value = "/app/15/token/experience/courseList", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo findExperienceCourseList(@RequestBody Map<String, String> map,
                                            @RequestHeader(value = "token", required = false) String token) {
        try {
            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token is required");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            //校验参数
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));
            Object obj_categoryId = map.get("categoryId");
            if (obj_categoryId == null) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("categoryId can not be null.");
                //打印出参
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Integer categoryId = TypeTrasUtil.ObjToInteger(obj_categoryId);
            List<ExperienceCourseDto> list = courseSearchService.findCourseByCategoryIdForExperience(tdNtMember.getMemb_id(), categoryId);
            RetInfo retInfo = RetInfo.getSuccessInfo();
            retInfo.setObj(list);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo(Lang.ZH);
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    @RequestMapping(value = "/app/15/courseCategoryList", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo findCourseCategoryList() {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo = courseSearchService.findCategory(null);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

}
